package com.niit.conteoller;

import com.niit.pojo.Goods;
import com.niit.pojo.Order;
import com.niit.pojo.Typing;
import com.niit.pojo.User;
import com.niit.service.GoodService;
import com.niit.service.OrderService;
import com.niit.service.TypeService;
import com.niit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Controller
public class AdminController {
    //管理员管理类目
    @Autowired
    private TypeService typeService;
@RequestMapping("/type_add")
public String aa(){
    return "type_add";
}
    //查询
    @RequestMapping("/type_list")
    public String selectList(Model model){
        List<Typing> typing=typeService.selectList();
//        System.out.println(typing);
        model.addAttribute("typing",typing);
        return "type_list";
    }

    //删除
    @RequestMapping("/typeDelete")
    public String typeDelete(Integer t_id){
//        System.out.println(t_id);
        typeService.typeDelete(t_id);
        return "redirect:/type_list";
    }

    //添加
    @RequestMapping("/addtype")
    public String typeInsert(HttpServletRequest request,Map<String,Object> map){
        String t_name=request.getParameter("t_name");
        String t_num=request.getParameter("t_num");
        Typing typing=new Typing(t_name,t_num);
        typeService.typeInsert(typing);
//        System.out.println(typing);
        List<Typing> typing1=typeService.selectList();
        map.put("typing",typing1);
        return "redirect:/type_list";
    }
//修改
    @RequestMapping("/type_update")
    public ModelAndView typeSelect(int t_id){
    //转发到页面
        ModelAndView mv=new ModelAndView();
        Typing typing = typeService.typeSelect(t_id);
        mv.addObject("typing",typing);
        return mv;
    }
    @RequestMapping("/typeupdate")
    public String typeUpdate(int t_id,HttpServletRequest request,Map<String,Object> map){
    String t_name=request.getParameter("t_name");
    String t_num=request.getParameter("t_num");
        Typing typing=new Typing(t_id,t_name,t_num);
        typeService.typeUpdate(typing);
        List<Typing> typing1=typeService.selectList();
        map.put("typing",typing1);
        return "redirect:/type_list";

    }

    //管理员管理商品
    @Autowired
    private GoodService goodService;

@RequestMapping("/good_add")
public String bb(){return "good_add";}

//查询
    @RequestMapping("/good_list")
   public String selectgoodtList(Model model){
       List<Goods> goods=goodService.selectgoodtList();
       System.out.println(goods);
       model.addAttribute("goods",goods);
       return "good_list";
   }

   //添加
    @RequestMapping("/addgood")
    public String goodInsert(HttpServletRequest request,Map<String,Object> map){
        int t_id= Integer.parseInt(request.getParameter("t_id"));
      /*  String cover=request.getParameter("cover");*/
        String name=request.getParameter("name");
        String intro=request.getParameter("intro");
        String spec=request.getParameter("spec");
        String price=request.getParameter("price");
        String stock=request.getParameter("stock");
//        String typing=request.getParameter("typing");
        String content=request.getParameter("content");
        Goods goods=new Goods(name,intro,spec,price,stock,content,t_id);
        goodService.goodInsert(goods);
        System.out.println(goods);
        List<Goods> goods1=goodService.selectgoodtList();
        map.put("goods",goods1);
        return "redirect:/good_list";
    }

    //删除
    @RequestMapping("/goodDelete")
    public String goodDelete(Integer id){
        System.out.println(id);
        goodService.goodDelete(id);
        return "redirect:/good_list";
    }

    //修改
    @RequestMapping("/good_update")
    public ModelAndView goodSelect(int id){
    //转发到页面
        ModelAndView mv=new ModelAndView();
        Goods goods=goodService.goodSelect(id);
        mv.addObject("goods",goods);
        return mv;
    }

    @RequestMapping("/goodupdate")
    public String goodUpdate(int id,HttpServletRequest request,Map<String,Object> map){
        String name=request.getParameter("name");
        String intro=request.getParameter("intro");
        String spec=request.getParameter("spec");
        String price=request.getParameter("price");
        String stock=request.getParameter("stock");
        String content=request.getParameter("content");
        int t_id= Integer.parseInt(request.getParameter("t_id"));
        System.out.println("打印"+t_id);
        Goods goods=new Goods(id,name,intro,spec,price,stock,content,t_id);
        goodService.goodUpdate(goods);
        List<Goods> goods1=goodService.selectgoodtList();
        System.out.println("打印"+goods1);
        map.put("goods",goods1);
        return "redirect:/good_list";
    }


    //管理员管理用户

    @RequestMapping("/user_add")
    public String cc(){
        return "user_add";
    }

    @Autowired
private UserService userService;

//查询
    @RequestMapping("/user_list")
    public String selectuserList(Model model){
        List<User> user=userService.selectuserList();
        model.addAttribute("user",user);
        return "user_list";
    }

    //删除
    @RequestMapping("/userDelete")
    public String userDelete(Integer id){
        userService.userDelete(id);
        return "redirect:/user_list";
    }

    //添加用户
    @RequestMapping("/adduser")
    public String userInsert(HttpServletRequest request,Map<String,Object> map){
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        String role=request.getParameter("role");
        User user=new User(name,password,role);
        userService.userInsert(user);
//        System.out.println(typing);
        List<User> user1=userService.selectuserList();
        map.put("user",user1);
        return "redirect:/user_list";
    }


    //修改信息
    @RequestMapping("/user_update")
    public ModelAndView userSelect(int id){
        //转发到页面
        ModelAndView mv=new ModelAndView();
        User user = userService.userSelect(id);
        mv.addObject("user",user);
        return mv;
    }
    @RequestMapping("/userupdate")
    public String userUpdate(int id,HttpServletRequest request,Map<String,Object> map){
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        User user=new User(id,name,password);
        userService.userUpdate(user);
        List<User> user1=userService.selectuserList();
        map.put("user1",user1);
        return "redirect:/user_list";

    }



    //管理员管理订单
    @Autowired
    private OrderService orderService;
    //查询
    @RequestMapping("/order_list")
    public String selectorderList(Model model){
        List<Order> order=orderService.selectorderList();
        System.out.println("打印"+order);
        model.addAttribute("order",order);
        return "order_list";
    }

    //删除
    @RequestMapping("/orderDelete")
    public String orderDelete(Integer o_id){
        orderService.orderDelete(o_id);
        return "redirect:/order_list";
    }
}
